var fs = require('fs');
var needle = require('needle');
var config = require('../../config.js');
var UPYun = require('./upyun.js').UPYun;

function md5(string) {
    var crypto = require('crypto');
    var md5sum = crypto.createHash('md5');
    md5sum.update(string, 'utf8');
    return md5sum.digest('hex');
}

function flush(_path) {
    needle.post('https://www.upyun.com/cp/api/purge/', {
        urls: config.file.upyun.domain + _path
    }, {
        headers: {
            'Referer': 'https://www.upyun.com/cp/',
            'Cookie': '__utma=4669943.1829680502.1385756994.1385756994.1387094922.2; __utmc=4669943; __utmz=4669943.1385756994.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmv=4669943.|1=Status=Logged-out=1; sid=o4tarehvsb2uhpa73qknqsg6r0; s=pvkv9A8uh%2F0vj36GIG1wou4%2BiAms4CaAZ9cf60iYn8Etu%2ByQ1AygOTijWZ06jKD9VJzBw4H402fTnGOVXcpLrugLmxpjwlk1i5v7ETRBkMAn2SSo%2B0TWpWh7U37HbYW17jXfbcupdOSm76HigdQsqZWPmpgZ%2FxK3lwQ%2BHg%2FPkBrOleXNIDGnF2MwplfETAz31mZWqrHRMe%2BcA%2BUWV8xJ',
            'user_agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36'
        }
    }, function (err, res, body) {
        console.log(!err && 'flush ok.');
    });
}

function upload(_path, file, callback, isContent) {
    var upyun = new UPYun(config.file.upyun.buck, config.file.upyun.user, config.file.upyun.password);
    if (isContent) {
        upyun.writeFile(_path, file, true, function (err, data) {
            if (err) {
                console.log(err);
            }
            callback();
            flush(_path);
        }, {});
    } else {
        fs.readFile(file, function (err, data) {
            upyun.writeFile(_path, data, true, function (err, data) {
                if (err) {
                    console.log(err);
                }
                callback();
            }, {});
        });
    }
}
module.exports = {
    upload: upload
};
// Test code

// 初始化空间
// var upyun = new UPYun("buckname", "username", "password");

//获取空间占用大小
// upyun.getBucketUsage(testCallback);

// upyun.getFolderUsage('/', testCallback);

// upyun.writeFile('/test.txt', '12323231', false, testCallback);

// upyun.writeFile('/test/test.txt', '12323231', true, testCallback);

// upyun.getFileInfo('/test.txt', testCallback);

// var fs =  require('fs');
// var fileContent = fs.readFileSync('test.jpg');
// var md5Str = md5(fileContent);
// upyun.setContentMD5(md5Str);
// upyun.setFileSecret('bac');
// upyun.writeFile('/test.jpg', fileContent, false, function(err, data){
//     if (!err) {
//         console.log(data);
//         console.log(upyun.getWritedFileInfo('x-upyun-width'));
//         console.log(upyun.getWritedFileInfo('x-upyun-height'));
//         console.log(upyun.getWritedFileInfo('x-upyun-frames'));
//         console.log(upyun.getWritedFileInfo('x-upyun-file-type'));
//     }
// });

// upyun.readFile('/test.txt', 'test.txt', testCallback);

// upyun.deleteFile('/test.txt', testCallback);

// upyun.mkDir('/test1', true, testCallback);

// upyun.rmDir('/test1', testCallback);

// upyun.readDir('/', testCallback);
//
//function testCallback(err, data) {
//    if (!err) {
//        console.log('Data: ');
//        console.log(data);
//    }
//    else {
//        console.log('Error: ');
//        console.log(err);
//    }
//}
